package com.eu.ebpm.engine.init;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eu.ebpm.engine.entity.deploy.Deploy;
import com.eu.ebpm.engine.entity.deploy.Mission;
import com.eu.ebpm.engine.entity.deploy.Transfer;
import com.eu.ebpm.engine.exception.ErrorCode;
import com.eu.ebpm.engine.exception.WorkFlowException;
import com.eu.ebpm.engine.pvm.EngineServiceType;

/**
 * 
 * 类名称: EbpmCache 
 * 类描述: 流程定义内容
 * 
 * @author 王晓华
 * @since 2013-12-23 下午07:42:28
 * @version v1.0
 */
public class EbpmCache {

	/**
	 * KEY - 流程列表
	 */
	private static Map<String, Deploy> keyDeploys = new HashMap<String, Deploy>();

	/**
	 * ID - 流程列表
	 */
	private static Map<Long, Deploy> deploys = new HashMap<Long, Deploy>();

	/**
	 * 任务列表 missionId ==> Mission
	 */
	private static Map<Long, Mission> missions = new HashMap<Long, Mission>();

	/**
	 * 任务指针列表
	 */
	private static Map<String, Transfer> transfers = new HashMap<String, Transfer>();

	public static void clearAll() {
		keyDeploys.clear();
		deploys.clear();
		missions.clear();
		transfers.clear();
	}

	public static Map<String, Deploy> getKeyDeploys() {
		return keyDeploys;
	}

	public static Map<Long, Deploy> getDeploys() {
		return deploys;
	}
	
	/**
	 * 或者可以启动的流程，不包括子流程
	 * @return
	 */
	public static List<Deploy> getRootDeploys() {
		List<Deploy> deploys = new ArrayList<Deploy>();
		for(Deploy d : EbpmCache.getDeploys().values()) {
			if(d.getParentId() == 0) {
				deploys.add(d);
			}
		}
		return deploys;
	}

	public static Map<Long, Mission> getMissions() {
		return missions;
	}

	public static Map<String, Transfer> getTransfers() {
		return transfers;
	}

	public static Deploy getDeployByKey(String key) {
		
		Deploy deploy = keyDeploys.get(key);
		
		if (deploy == null) {
			throw new WorkFlowException(ErrorCode.RU_NOTFOUND_DEPLOY, key);
		}
		
		return deploy;
	}

	public static Deploy getDeploy(Long deployId) {
		return deploys.get(deployId);
	}

	public static Mission getMission(Long missionId) {
		return missions.get(missionId);
	}

	public static Transfer getTransfer(Long key) {
		return transfers.get(key);
	}
	
	/**
	 * 根据 missionId 获取 transfers
	 * @param missionId
	 * @return
	 */
	public static List<Transfer> getTransferByMissionId(Long missionId) {
		List<Transfer> transfers = EbpmCache.getMission(missionId).getTransfers();
		
		if(transfers.size() == 1) {
			
			// 【下个节点】
			Mission toMission1 = transfers.get(0).getToMission();
			
			// 1、【下个节点】是FORK_AFTER，则获取FORK_AFTER后面的执行条件
			if(EngineServiceType.FORK_AFTER == toMission1.getType()) {
				transfers = toMission1.getTransfers();
			} 
			// 2、【下个节点】是【LANE_AFTER】【SUB_END_EVENT】
			else if(EngineServiceType.LANE_AFTER == toMission1.getType() || EngineServiceType.SUB_END_EVENT == toMission1.getType()) {
				// 2.1 - 获取【下个节点】的执行路线
				List<Transfer> transfers2 = toMission1.getTransfers();
				
				if(transfers2.size() == 1) {
					// 2.2 - 【｛下下｝个节点】如果是FORK_AFTER
					Mission toMission2 = transfers2.get(0).getToMission();
					if(toMission2.getType() == EngineServiceType.FORK_AFTER) {
						transfers = toMission2.getTransfers();
					}
				}
			}
		}
		
		return transfers;
	}
	
	public static void main(String[] args) {
		Long key1 = 88888L;
		Long key2 = 88888L;
		missions.put( key1, new Mission());
		missions.put( key2, new Mission());
		
		System.out.println(missions);
		
		Integer i1 = 6666;
		Integer i2 = 6666;
		System.out.println(i1 < i2);
	}

}
